<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2021-11-23 09:58:02
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-12 16:33:36
-->
<template>
  <div>
    <el-dialog
      title="新增地址"
      :visible.sync="dialog.addressShow"
      width="30%"
      :modal="false"
    >
      <el-form
        :model="addressForm"
        ref="addressForm"
        :rules="addressRules"
        label-position="top"
      >
        <el-form-item label="收件人" prop="addressee">
          <el-input
            v-model="addressForm.addressee"
            placeholder="请输入收件人"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input
            v-model="addressForm.phone"
            placeholder="请输入手机号"
            size="normal"
          ></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input
            v-model="addressForm.address"
            placeholder="请填写详细收件地址,例如街道名称、门牌号码、楼层
等信息"
            clearable
            type="textarea"
            :rows="4"
          ></el-input>
        </el-form-item>
        <el-form-item label="默认地址">
          <el-checkbox v-model="addressForm.isDefault">默认</el-checkbox>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="dialog.addressShow = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { insertInvoiceAddress, updateInvoiceAddress } from "@/api/enterprise/invoiceAst";
export default {
  props: {
    dialog: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      addressForm: {
        addressee: "",
        phone: "",
        address: "",
        isDefault: false,
      },
      addressRules: {
        addressee: [
          {
            required: true,
            message: "请输入收件人",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入详细地址",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    onSubmit() {
      this.$refs.addressForm.validate((valid) => {
        if (!valid) return;
        if (!this.addressForm.id) {
          insertInvoiceAddress(this.addressForm).then((res) => {
            this.$message.success(res.msg);
            this.addressShow = false;
            this.$emit("addList", this.addressForm);
            (this.addressForm = {
              addressee: "",
              cantact: "",
              detailAddress: "",
              checked: "",
            }),
              (this.dialog.addressShow = false);
            this.getaddList();
          });
        } else {
          updateInvoiceAddress(this.addressForm).then((res) => {
            this.$message.success(res.msg);
            this.addressShow = false;
            this.$emit("addList", this.addressForm);
            (this.addressForm = {
              addressee: "",
              cantact: "",
              detailAddress: "",
              checked: "",
            }),
              (this.dialog.addressShow = false);
            this.getaddList();
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>